package com.guigu.youxuan.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.pojo.Gather;
import com.guigu.youxuan.pojo.Outbound;
import com.guigu.youxuan.pojo.OutboundDetail;
import com.guigu.youxuan.pojo.dto.OutboundAndDetailsDto;
import com.guigu.youxuan.pojo.dto.OutboundDto;
import com.guigu.youxuan.pojo.dto.SupplierGoodsDto;
import com.guigu.youxuan.service.IGatherService;
import com.guigu.youxuan.service.IOutboundService;
import com.guigu.youxuan.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/outbound")
public class OutboundController {
    @Autowired
    private IOutboundService iOutboundService;

    @Autowired
    private NumberUtils numberUtils;

    /**
     * 出库审核查询状态为S001-1待审核的信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/OutStorageFuhePage/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Outbound> OutStorageFuhePage(
            @PathVariable int currentPage,
            @PathVariable int pageSize){
        PageInfo<Outbound> page=this.iOutboundService.OutStorageFuhePage(currentPage,pageSize);
        return page;
    }

    /**
     * 出库列表查询所有出库信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/OutStorageListPage/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Outbound> OutStorageListPage(
            @PathVariable int currentPage,
            @PathVariable int pageSize){
        PageInfo<Outbound> page=this.iOutboundService.OutStorageListPage(currentPage,pageSize);
        return page;
    }
    /**
     * 根据出库表id查询对应的出库信息
     * @param id
     * @return
     */
    @RequestMapping("/OutStorageReviewListByid/{id}")
    @ResponseBody
    public Outbound OutStorageReviewListByid(@PathVariable int id){
        Outbound warehouse=this.iOutboundService.getById(id);
        return warehouse;
    }
    /**
     * 出库审核信息复核未通过
     * @param outbound
     * @return
     */
    @RequestMapping("/NoFuheReviewList")
    @ResponseBody
    public String NoFuheReviewList(@RequestBody Outbound outbound) {
        this.iOutboundService.updateById(outbound);
        outbound.setCheckTag("S001-2");
        System.out.println("审核"+outbound.getCheckTag()+"审核");
        System.out.println("id"+outbound.getWarehouseId()+"id");
        boolean flag=this.iOutboundService.updateById(outbound);
        if(flag){
            return "ok";
        }
        return "error";
    }

    /**
     * 出库审核信息复核通过
     * @param outboundDto
     * @return
     */
    @RequestMapping("/YesFuheReviewList")
    @ResponseBody
    public String YesFuheReviewList(@RequestBody OutboundDto outboundDto) {
        iOutboundService.YesFuheReviewList(outboundDto);
        return "出库审核成功";
    }

    /**
     * 出库单制定
     * @param dto
     * @return
     */
    @RequestMapping("/outboundMake")
    @ResponseBody
    public String  outboundMake(@RequestBody OutboundAndDetailsDto dto) {
        int flag =0;
        List<OutboundDetail> list = dto.getOutboundDetailList();
        Map<String ,List<OutboundDetail>> map = new HashMap<>();
        for(OutboundDetail outboundDetail : list) {
            String merchantId = outboundDetail.getMerchantId();
            // 如果map中还没有这个merchantId的列表，则创建一个新的列表
            map.putIfAbsent(merchantId, new ArrayList<>());
            // 将outboundDetail添加到对应的列表中
            map.get(merchantId).add(outboundDetail);
        }
        for (Map.Entry<String, List<OutboundDetail>> entry : map.entrySet()) {
            // 获取键（merchantId）
            String merchantId = entry.getKey();

            // 获取与键关联的OutboundDetail对象列表
            List<OutboundDetail> outboundDetailList = entry.getValue();

            Outbound outbound = new Outbound();
            String payOrderNumber = numberUtils.getTypeNumber("400");
            outbound.setPayOrderNumber(payOrderNumber);
            outbound.setRegister(dto.getOutbound().getRegister());
            outbound.setReason(dto.getOutbound().getReason());
            outbound.setTotalPrice(0.00);
            outbound.setTotalAmount(0);

            // 遍历列表中的每个OutboundDetail对象
            for (OutboundDetail outboundDetail : outboundDetailList) {
                outbound.setTotalAmount(outbound.getTotalAmount()+outboundDetail.getAmount());
                outbound.setTotalPrice(outboundDetail.getPrice()*outboundDetail.getAmount()+outbound.getTotalPrice());
            }

            //生成关联订单
            String orderList = outboundDetailList.stream()
                    .map(OutboundDetail::getOrdersId) // 映射出每个OutboundDetail的OrdersId，返回的是List<String>
                    .distinct() // 去除重复的orderId
                    .collect(Collectors.joining(","));

            outbound.setOrderList(orderList);

            iOutboundService.createOutbound(outbound,outboundDetailList);
            flag++;
        }

        return "已生成"+flag+"条出库单，请前往审核！";
    }
}
